//模拟翻牌--失败
//typedef list is not a type
#include<Arduino.h>
#include<stdio.h>
#include<stdlib.h>

typedef struct _node{
  int value;
  struct _node *next;
} Node;

int LEDS[] = {3,5,7};
int BUTTONS[] = {2,4,6};
int score = 0;
bool gameStarted = false;
unsigned long gameStartTime = 0;
const unsigned long gameDuration = 6000;

typedef struct _List{
  Node *head;
  Node *tail;
} List;
List list;
list.tail =list.head = (Node*)malloc(sizeof(Node));
list.tail->next = list.head->next =NULL;
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
  for(int i = 0;i<3;i++){
    pinMode(LEDS[i],OUTPUT);
    pinMode(BUTTONS[i],INPUT_PULLUP);
  }
}

void loop() {
  // put your main code here, to run repeatedly:
if(!gameStarted&&digitalRead(BUTTONS[0])==LOW&&digitalRead(BUTTONS[1])==LOW&&digitalRead(BUTTONS[2])==LOW){
  Serial.println("游戏开始");
  gameStarted = true;
  score = 0;
  gameStartTime = millis();
  delay(1000);
}
if(gameStarted&&millis()-gameStartTime<gameDuration){
    int led = random(0,3);
    list.head->value = led;
  while(millis()-gameStartTime<gameDuration){ 
    int led = random(0,3);
    addlast(&list,led);
    digitalWrite(LEDS[led],HIGH);
    delay(5000);
  }
}
Node *p = (Node*)malloc(sizeof(Node));
for(p=list.head;p;p=p->next)){
  if(digitalRead(BUTTONS[p->value]==LOW)){
    score++;
    Serial.println("得分+1");
  }
  delay(1000);
}
printlist(&list);
  Serial.print("游戏结束，你的得分：");
  Serial.println(score);
  delay(3000);
}
void addlast(List*plist,int number){
  Node *p = (Node*)malloc(sizeof(Node));
          p->value = number;
          p->next = NULL;
          plist->tail->next = p;
          plist->tail = p;
}
void printlist(List*plist){
  Node *p;
  Serial.println("灯亮起的顺序是：");
  for(p = plist->head;p;p->next){
    Serial.print(p->value);
  }
  Serial.println();

}
